// 前端本地 Mock：盘点单 API
// 与现有 API 结构保持一致，便于后续无缝切换到后端

export const useSysstocktakingorderApi = () => {
  return baseApi;
};

// ------- 本地实现 -------
type Order = Sysstocktakingorder;
const mockDb: Order[] = [
  {
    id: 2100000000101,
    code: 'PDDH8944',
    inventoryResult: '草稿',
    warehouseName: '货品仓库一',
    stockTakingType: '存货盘点',
    startTime: '2024-02-29 00:00:00',
    endTime: '2024-02-29 00:00:00',
    outStatus: 4, // NotOutOfWarehouse
    inStatus: 4,
    goodsName: '三锅头、可笑、何必等8项……',
    quantity: 50,
    documentStatus: 1, // Draft
    remark: '草稿状态盘点单',
    tenantId: 1300000000001,
    orgId: 1300000000001,
    isDelete: false,
    deleteTime: null as any,
    createTime: '2024-02-29 09:00:00',
    updateTime: null as any,
    createUserId: 1300000000001,
    createUserName: '任杰',
    updateUserId: null as any,
    updateUserName: null as any,
  },
  {
    id: 2100000000102,
    code: 'PDDH9896',
    inventoryResult: '有盈亏',
    warehouseName: '货品仓库一',
    stockTakingType: '存货盘点',
    startTime: '2024-02-29 00:00:00',
    endTime: '2024-02-29 00:00:00',
    outStatus: 5, // PartiallyOutOfWarehouse
    inStatus: 5,
    goodsName: '三锅头、可笑、何必等8项……',
    quantity: 50,
    documentStatus: 2, // PendingReview
    remark: '待审核状态盘点单',
    tenantId: 1300000000001,
    orgId: 1300000000001,
    isDelete: false,
    deleteTime: null as any,
    createTime: '2024-02-29 10:00:00',
    updateTime: null as any,
    createUserId: 1300000000001,
    createUserName: '任杰',
    updateUserId: null as any,
    updateUserName: null as any,
  },
  {
    id: 2100000000103,
    code: 'PDDH3629',
    inventoryResult: '无盈亏',
    warehouseName: '货品仓库一',
    stockTakingType: '存货盘点',
    startTime: '2024-02-29 00:00:00',
    endTime: '2024-02-29 00:00:00',
    outStatus: 6, // Completed
    inStatus: 6,
    goodsName: '三锅头、可笑、何必等8项……',
    quantity: 50,
    documentStatus: 6, // Completed
    remark: '已完成状态盘点单',
    tenantId: 1300000000001,
    orgId: 1300000000001,
    isDelete: false,
    deleteTime: null as any,
    createTime: '2024-02-29 11:00:00',
    updateTime: null as any,
    createUserId: 1300000000001,
    createUserName: '任杰',
    updateUserId: null as any,
    updateUserName: null as any,
  },
];

const delay = (ms: number) => new Promise(r => setTimeout(r, ms));

const baseApi = {
  page: async (params: any) => {
    await delay(120);
    const { page = 1, pageSize = 20 } = params || {};
    const start = (page - 1) * pageSize;
    const items = mockDb.slice(start, start + pageSize);
    return {
      data: { result: { total: mockDb.length, items } },
    } as any;
  },
  list: async () => ({ data: { result: mockDb } } as any),
  detail: async (id: number) => {
    await delay(60);
    const item = mockDb.find(x => x.id === id);
    return { data: { result: item } } as any;
  },
  add: async (data: Order) => {
    data.id = Date.now();
    mockDb.unshift(data);
    return { data: { result: true } } as any;
  },
  update: async (data: Order) => {
    const i = mockDb.findIndex(x => x.id === data.id);
    if (i >= 0) mockDb[i] = { ...mockDb[i], ...data };
    return { data: { result: true } } as any;
  },
  delete: async (id: number) => {
    const i = mockDb.findIndex(x => x.id === id);
    if (i >= 0) mockDb.splice(i, 1);
    return { data: { result: true } } as any;
  },
  batchDelete: async (ids: number[]) => {
    ids.forEach(id => {
      const i = mockDb.findIndex(x => x.id === id);
      if (i >= 0) mockDb.splice(i, 1);
    });
    return { data: { result: true } } as any;
  },
  importData: async () => ({ data: { result: true } } as any),
  downloadTemplate: async () => ({ data: { result: true } } as any),
};

// 实体定义（与其他 API 文件风格一致）
export interface Sysstocktakingorder {
  id: number;
  code?: string;
  inventoryResult?: string;
  warehouseName?: string;
  stockTakingType?: string;
  startTime?: string;
  endTime?: string;
  outStatus?: number;
  inStatus?: number;
  goodsName?: string;
  quantity?: number;
  documentStatus?: number;
  remark?: string;
  tenantId: number;
  orgId: number;
  isDelete?: boolean;
  deleteTime?: string | null;
  createTime?: string;
  updateTime?: string | null;
  createUserId?: number;
  createUserName?: string;
  updateUserId?: number | null;
  updateUserName?: string | null;
}


